package es.ats.codington.web.servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import es.ats.codington.festival.domain.Place;
import es.ats.codington.festival.exceptions.ServiceException;
import es.ats.codington.festival.services.PlaceService;

/**
 * Servlet implementation class UpdatePlaceServlet
 */
@WebServlet("/UpdatePlaceServlet")
public class UpdatePlaceServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdatePlaceServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		 
		HttpSession session = request.getSession(true);
		int idPlace = 0;
		
		
		/**
		 * Parameters obtained form
		 */
		String pn = request.getParameter("placename");
		String pd = request.getParameter("placedescription");
		String c = request.getParameter("capacityplace");
		String pz = request.getParameter("placezones");
		//String pt = request.getParameter("placetypes");
		
		
		
		int cN = Integer.parseInt(c);
		
		idPlace = (Integer) session.getAttribute("idPlaceAdminSession");
	
		PlaceService service = new PlaceService();
		Place place = new Place();
		
	    place.setNamePlace(pn);
	    place.setPlaceDescription(pd);
	    place.setCapacityPlace(cN);
	    place.setZonePlace(pz);
	    place.setId(idPlace);
	
		
	    
	    try {
	    	/**
	    	 * Update a place
	    	 */
	    	service.updatePlace(place);
	    	
	    	/**
			 * Invokes the action "adminvisitor.jsp"
			 */
	    	response.sendRedirect("ShowPlacesAdminServlet");
	    	//request.getRequestDispatcher("ShowPlacesAdminServlet").forward(request, response);
	    	
	    	
		} catch (ServiceException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		} 
	}

}
